﻿using EduCommon;
using EduModel;
using System;
using System.Linq;
using System.Web.Mvc;
namespace EduCenter.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Teacher/

        public ActionResult Student()
        {
            using (EduContext db = new EduContext())
            {
                var school = db.T_School.ToList();
                return View(school);
            }
        }

        public ActionResult GetPageing(int page = 1, int limit = 10, string realName = "", string loginName = "", int schoolId = 0)
        {
            int total = 0;

            int SchoolId = CurUser.User.SchoolId;
            int GradeId = CurUser.User.GradeId;
            int ClassId = CurUser.User.ClassId;

            using (EduContext db = new EduContext())
            {
                var users = from user in db.T_User
                            join school in db.T_School on user.SchoolId equals school.SchoolId
                            join grade in db.T_TreeGrade on user.GradeId equals grade.GradeId
                            join classes in db.T_Class on user.ClassId equals classes.ClassId
                            orderby user.UserId
                            select new
                            {
                                user.UserId,
                                user.UserState,
                                user.Sex,
                                user.SchoolId,
                                user.GradeId,
                                user.ClassId,
                                user.AreaCode,
                                user.LoginName,
                                user.RealName,
                                user.HeadImg,
                                user.RoleId,
                                school.SchoolName,
                                grade.GradeName,
                                classes.ClassName
                            };
                if (realName.Length > 0)
                {
                    users = users.Where(u => u.RealName.Contains(realName));
                }
                if (loginName.Length > 0)
                {
                    users = users.Where(u => u.LoginName.Contains(loginName));
                }
                if (schoolId > 0)
                {
                    users = users.Where(u => u.SchoolId == schoolId);
                }
                total = users.Count();
                var paper = users.Take(limit * page).Skip(limit * (page - 1)).ToList();
                return Json(new { status = Result.success, msg = total == 0 ? "暂无数据" : "", count = total, data = paper }, JsonRequestBehavior.AllowGet);
            }

        }

        [HttpPost]
        public ActionResult SetState(int id)
        {
            using (EduContext db = new EduContext())
            {
                var user = db.T_User.SingleOrDefault(t => t.UserId == id);
                user.UserState = user.UserState == 1 ? 0 : 1;
                int result = db.SaveChanges();
                return Json(new { status = result > 0 ? Result.success : Result.fail });
            }
        }

        public ActionResult GetUser(int id)
        {
            /*
            try
            {
                using (EduContext db = new EduContext())
                {
                    var vm = (from user in db.T_User
                          join school in db.T_School on user.SchoolId equals school.SchoolId
                          join grade in db.T_TreeGrade on user.GradeId equals grade.GradeId
                          join classes in db.T_Class on user.ClassId equals classes.ClassId
                          where user.UserId == id
                          select new { User = user, GradeName = grade.GradeName, SchoolName = school.SchoolName, ClassName = classes.ClassName })
                           .Select(t => new UserViewModel { User = t.User, GradeName = t.GradeName, SchoolName = t.SchoolName, ClassName = t.ClassName }).SingleOrDefault();
                return View(vm);
                    }

            }
            catch (Exception ex)
            {

                return Json(new { status = 10000, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
            */
            return View();
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            try
            {
                using (EduContext db = new EduContext())
                {
                    var user = db.T_User.SingleOrDefault<T_User>(t => t.UserId == id);
                    if (user != null) db.T_User.Remove(user);
                    int result = db.SaveChanges();

                    return Json(new { status = result > 0 ? Result.success : Result.fail });
                }
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    status = Result.error,
                    message = ex.Message
                });
            }
        }

        [HttpPost]
        public ActionResult DeleteAll(int[] id)
        {
            try
            {
                using (EduContext db = new EduContext())
                {
                    var users = db.T_User.Where(t => id.Contains(t.UserId));
                    db.T_User.RemoveRange(users);
                    int result = db.SaveChanges();
                    return Json(new { status = result > 0 ? Result.success : Result.fail });
                }
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    status = Result.error,
                    message = ex.Message
                });
            }

        }
    }
}
